package me.flyray.crm.core.biz;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.flyray.crm.core.entity.MerchantAccount;
import me.flyray.crm.core.entity.MerchantAccountJournal;
import me.flyray.crm.core.entity.PersonalAccount;
import me.flyray.crm.core.entity.PersonalAccountJournal;
import me.flyray.crm.core.mapper.MerchantAccountJournalMapper;
import me.flyray.crm.core.mapper.MerchantAccountMapper;
import me.flyray.crm.core.mapper.PersonalAccountJournalMapper;
import me.flyray.crm.core.mapper.PersonalAccountMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.wxiaoqi.security.biz.common.crm.request.AccountJournalQueryRequest;
import com.github.wxiaoqi.security.biz.common.crm.request.AccountQueryRequest;
import com.github.wxiaoqi.security.biz.common.crm.request.AccountingOrderInfoReq;
import com.github.wxiaoqi.security.biz.common.enums.InOutFlagEnums;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.common.entity.MerchantAccountEntity;
import com.github.wxiaoqi.security.common.entity.PersonalAccountEntity;
import com.github.wxiaoqi.security.common.enums.CustomerTypeEnums;
import com.github.wxiaoqi.security.common.util.EntityUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 个人或商户账户信息查询
 */
@Slf4j
@Transactional(rollbackFor = Exception.class)
@Service
public class CommonAccountQueryBiz {
	@Autowired
	private PersonalAccountMapper personalAccountMapper;
	@Autowired 
	private PersonalAccountJournalMapper personalAccountJournalMapper;

	@Autowired
	private MerchantAccountMapper merchantAccountMapper;
	@Autowired
	private MerchantAccountJournalMapper merchantAccountJournalMapper;
	
	@Value("${bountyHunter.balanceSaltValue}")
	private String balanceSaltValue;
	/**
	 * 账户查询接口
	 */
	public Map<String, Object> accountQuery(AccountQueryRequest accountQueryRequest) {
		log.info("个人或者商户账户信息查询-请求参数：{}" + EntityUtils.beanToMap(accountQueryRequest));
		Map<String, Object> response = new HashMap<String, Object>();
		try {
			if(CustomerTypeEnums.CUST_MERCHANT.getCode().equals(accountQueryRequest.getCustomerType())){//商户账户
				MerchantAccount queryMerAccountparam=new MerchantAccount();
				queryMerAccountparam.setPlatformId(accountQueryRequest.getPlatformId());//添加平台约束
				queryMerAccountparam.setMerchantId(accountQueryRequest.getMerchantId());
				if(!StringUtils.isEmpty(accountQueryRequest.getMerchantType())){
					queryMerAccountparam.setMerchantType(accountQueryRequest.getMerchantType());
				}
				if(!StringUtils.isEmpty(accountQueryRequest.getAccountType())){
					/**
					 * 获取指定账户类型的商户账户信息
					 * */
					queryMerAccountparam.setAccountType(accountQueryRequest.getAccountType());
					MerchantAccount merchantAccount=merchantAccountMapper.selectOne(queryMerAccountparam);
					if(merchantAccount==null){
						response.put("code", BizResponseCode.MER_ACC_NOTEXIST.getCode());
						response.put("message", BizResponseCode.MER_ACC_NOTEXIST.getMessage());
						log.info("个人或者商户账户信息查询 返回参数。。。。。。{}", response);
						return response;
					}
					MerchantAccountEntity merchantAccountEntity = new MerchantAccountEntity();
					BeanUtils.copyProperties(merchantAccount, merchantAccountEntity);
					response.put("accountInfo",merchantAccountEntity);
				}else{
					/***
					 * 获取全部的商户账户信息
					 * */
					List<MerchantAccount>merchantAccounts=merchantAccountMapper.select(queryMerAccountparam);
					if(merchantAccounts==null || merchantAccounts.size()<1){
						response.put("code", BizResponseCode.MER_ACC_NOTEXIST.getCode());
						response.put("message", BizResponseCode.MER_ACC_NOTEXIST.getMessage());
						log.info("个人或者商户账户信息查询 返回参数。。。。。。{}", response);
						return response;
					}
					response.put("accountInfo",merchantAccounts);
				}
				
			}
			if(CustomerTypeEnums.CUST_PERSONAL.getCode().equals(accountQueryRequest.getCustomerType())){//个人账户
				PersonalAccount queryPerAccountparam=new PersonalAccount();
				queryPerAccountparam.setPlatformId(accountQueryRequest.getPlatformId());
				queryPerAccountparam.setPersonalId(accountQueryRequest.getPersonalId());
				if(!StringUtils.isEmpty(accountQueryRequest.getAccountType())){
					queryPerAccountparam.setAccountType(accountQueryRequest.getAccountType());
					PersonalAccount personalAccount=personalAccountMapper.selectOne(queryPerAccountparam);
					PersonalAccountEntity personalAccountEntity = new PersonalAccountEntity();
					if (null != personalAccount) {
						BeanUtils.copyProperties(personalAccount, personalAccountEntity);
					}else{
						personalAccountEntity.setPlatformId(accountQueryRequest.getPlatformId());
						personalAccountEntity.setPersonalId(accountQueryRequest.getPersonalId());
						personalAccountEntity.setAccountType(accountQueryRequest.getAccountType());
						personalAccountEntity.setAccountBalance(BigDecimal.ZERO);
					}
					response.put("accountInfo",personalAccountEntity);
				}else{
					List<PersonalAccount>personalAccounts=personalAccountMapper.select(queryPerAccountparam);
					response.put("accountInfo",personalAccounts);
				}
			}
			response.put("code", BizResponseCode.OK.getCode());
			response.put("message", BizResponseCode.OK.getMessage());
			log.info("个人或者商户账户信息查询 返回参数。。。。。。{}", response);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("个人或者商户账户信息查询  报错--"+e.getMessage());
			response.put("code", BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			response.put("message", BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return response;
		}
		return response;
	}
	
	/**
	 * 查询账户交易流水
	 * @author centerroot
	 * @time 创建时间:2018年9月27日下午3:57:14
	 * @param accountJournalQueryRequest
	 * @return
	 */
	public Map<String, Object> accountJournalQuery(AccountJournalQueryRequest accountJournalQueryRequest) {
		log.info("个人或者商户查询账户交易流水-请求参数：{}" + EntityUtils.beanToMap(accountJournalQueryRequest));
		Map<String, Object> response = new HashMap<String, Object>();
		try {
			if(CustomerTypeEnums.CUST_MERCHANT.getCode().equals(accountJournalQueryRequest.getCustomerType())){//商户账户
				MerchantAccountJournal merchantAccountJournalReq = new MerchantAccountJournal();
				merchantAccountJournalReq.setPlatformId(accountJournalQueryRequest.getPlatformId());
				merchantAccountJournalReq.setMerchantId(accountJournalQueryRequest.getMerchantId());
				merchantAccountJournalReq.setAccountType(accountJournalQueryRequest.getAccountType());
				List<MerchantAccountJournal> merchantAccountJournals = merchantAccountJournalMapper.select(merchantAccountJournalReq);
				response.put("accountJournalInfo",merchantAccountJournals);
			}
			if(CustomerTypeEnums.CUST_PERSONAL.getCode().equals(accountJournalQueryRequest.getCustomerType())){//个人账户
				PersonalAccountJournal personalAccountJournalReq = new PersonalAccountJournal();
				personalAccountJournalReq.setPlatformId(accountJournalQueryRequest.getPlatformId());
				personalAccountJournalReq.setPersonalId(accountJournalQueryRequest.getPersonalId());
				personalAccountJournalReq.setAccountType(accountJournalQueryRequest.getAccountType());
				List<PersonalAccountJournal> personalAccountJournals = personalAccountJournalMapper.select(personalAccountJournalReq);
				response.put("accountJournalInfo",personalAccountJournals);
			}
			response.put("code", BizResponseCode.OK.getCode());
			response.put("message", BizResponseCode.OK.getMessage());
			log.info("个人或者商户查询账户交易流水 返回参数。。。。。。{}", response);
		} catch (Exception e) {
			e.printStackTrace();
			log.info("个人或者商户查询账户交易流水  报错--"+e.getMessage());
			response.put("code", BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			response.put("message", BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return response;
		}
		return response;
	}
	
	/**
	 * 查询订单状态
	 * 1、判断是余额账户 ， 交易类型为支付 ， 查询用户出账账务明细和商户入账账务明细
	 * 2、判断是余额账户 ， 交易类型为退款 ， 查询用户入账账务明细和商户出账账务明细
	 * 3、判断不是余额账户 ， 交易类型为支付 ， 查询商户入账账务明细
	 * 4、判断不是余额账户 ， 交易类型为退款 ， 查询商户出账账务明细
	 * 5、如果查询结果不为空返回200，如果查询结果为空返回错误码
	 * @author yyn
	 * @time 创建时间:2018年12月28日
	 * @param accountingOrderInfoReq
	 * @return
	 */
	public Map<String, Object> queryOrderInfo(AccountingOrderInfoReq accountingOrderInfoReq){
		
		log.info("查询订单状态请求参数：{}" + EntityUtils.beanToMap(accountingOrderInfoReq));
		Map<String, Object> baseResponse =  new HashMap<String, Object>();
		
		// 判断是否为余额账户
		if ("1".equals(accountingOrderInfoReq.getIsBalance())) {
			// 是余额账户
			if ("01".equals(accountingOrderInfoReq.getTradeType())) {
				// 支付
				PersonalAccountJournal personalAccountJournalReq = new PersonalAccountJournal();
				personalAccountJournalReq.setOrderNo(accountingOrderInfoReq.getOrderNo());
				personalAccountJournalReq.setInOutFlag(InOutFlagEnums.out.getCode());// 出账
				personalAccountJournalReq.setTradeType(accountingOrderInfoReq.getTradeType());
				PersonalAccountJournal personalAccountJournalResp = personalAccountJournalMapper.selectOne(personalAccountJournalReq);
				
				MerchantAccountJournal merchantAccountJournalReq = new MerchantAccountJournal();
				merchantAccountJournalReq.setOrderNo(accountingOrderInfoReq.getOrderNo());
				merchantAccountJournalReq.setInOutFlag(InOutFlagEnums.in.getCode());// 入账
				merchantAccountJournalReq.setTradeType(accountingOrderInfoReq.getTradeType());
				MerchantAccountJournal merchantAccountJournalRsp = merchantAccountJournalMapper.selectOne(merchantAccountJournalReq);
				
				if (personalAccountJournalReq != null && merchantAccountJournalRsp != null){
					baseResponse.put("code",BizResponseCode.OK.getCode());
					baseResponse.put("message",BizResponseCode.OK.getMessage());
				} else {
					baseResponse.put("code",BizResponseCode.ACCOUNT_TRANSACTION_STATUS_FAILED.getCode());
					baseResponse.put("message",BizResponseCode.ACCOUNT_TRANSACTION_STATUS_FAILED.getMessage());
				}
				
			}else{
				// 退款
				PersonalAccountJournal personalAccountJournalReq = new PersonalAccountJournal();
				personalAccountJournalReq.setOrderNo(accountingOrderInfoReq.getOrderNo());
				personalAccountJournalReq.setInOutFlag(InOutFlagEnums.in.getCode());// 入账
				personalAccountJournalReq.setTradeType(accountingOrderInfoReq.getTradeType());
				PersonalAccountJournal personalAccountJournalRsp = personalAccountJournalMapper.selectOne(personalAccountJournalReq);
				
				MerchantAccountJournal merchantAccountJournalReq = new MerchantAccountJournal();
				merchantAccountJournalReq.setOrderNo(accountingOrderInfoReq.getOrderNo());
				merchantAccountJournalReq.setInOutFlag(InOutFlagEnums.out.getCode());// 出账
				merchantAccountJournalReq.setTradeType(accountingOrderInfoReq.getTradeType());
				MerchantAccountJournal merchantAccountJournalRsp = merchantAccountJournalMapper.selectOne(merchantAccountJournalReq);
				
				if (personalAccountJournalReq != null && merchantAccountJournalRsp !=null) {
					baseResponse.put("code",BizResponseCode.OK.getCode());
					baseResponse.put("message",BizResponseCode.OK.getMessage());
				} else {
					baseResponse.put("code",BizResponseCode.ACCOUNT_TRANSACTION_STATUS_FAILED.getCode());
					baseResponse.put("message",BizResponseCode.ACCOUNT_TRANSACTION_STATUS_FAILED.getMessage());
				}
			}
		} else {
			// 不是余额账户
			if ("01".equals(accountingOrderInfoReq.getTradeType())) {
				// 支付
				MerchantAccountJournal merchantAccountJournalReq = new MerchantAccountJournal();
				merchantAccountJournalReq.setOrderNo(accountingOrderInfoReq.getOrderNo());
				merchantAccountJournalReq.setInOutFlag(InOutFlagEnums.in.getCode());// 入账
				merchantAccountJournalReq.setTradeType(accountingOrderInfoReq.getTradeType());
				MerchantAccountJournal merchantAccountJournalRsp = merchantAccountJournalMapper.selectOne(merchantAccountJournalReq);
				
				if (merchantAccountJournalRsp != null) {
					baseResponse.put("code",BizResponseCode.OK.getCode());
					baseResponse.put("message",BizResponseCode.OK.getMessage());
				} else {
					baseResponse.put("code",BizResponseCode.ACCOUNT_TRANSACTION_STATUS_FAILED.getCode());
					baseResponse.put("message",BizResponseCode.ACCOUNT_TRANSACTION_STATUS_FAILED.getMessage());
				}
			}else{
				// 退款
				MerchantAccountJournal merchantAccountJournalReq = new MerchantAccountJournal();
				merchantAccountJournalReq.setOrderNo(accountingOrderInfoReq.getOrderNo());
				merchantAccountJournalReq.setInOutFlag(InOutFlagEnums.out.getCode());// 出账
				merchantAccountJournalReq.setTradeType(accountingOrderInfoReq.getTradeType());
				MerchantAccountJournal merAccountingDetailResp = merchantAccountJournalMapper.selectOne(merchantAccountJournalReq);
				
				if (merAccountingDetailResp != null) {
					baseResponse.put("code",BizResponseCode.OK.getCode());
					baseResponse.put("message",BizResponseCode.OK.getMessage());
				} else {
					baseResponse.put("code",BizResponseCode.ACCOUNT_TRANSACTION_STATUS_FAILED.getCode());
					baseResponse.put("message",BizResponseCode.ACCOUNT_TRANSACTION_STATUS_FAILED.getMessage());
				}
			}
		}
		
		log.info("查询订单状态参数：{}" + EntityUtils.beanToMap(baseResponse));
		return baseResponse;
	}
	
}
